<?php
require_once('../model/changeArt.php');
$article = selectArt($_GET['aid']);
$category = allBoard();
$artBoard = byIdAllBoard($article['board_id']);
?>

<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <title>修改文章信息</title>
  <link rel="stylesheet" href="../style/css/changeArt.css">
</head>

<body>
  <div class="container">
    <div class="header">
      <a class="header-title" href="../view/index.php">说点吧</a>
      <span>个人中心</span>
    </div>
    <h2>修改文章信息</h2>
    <?php if (isset($_GET['success'])): ?>
      <div class="alert <?= $_GET['success'] == 1 ? 'alert-success' : 'alert-error' ?>">
        <?= $_GET['success'] == 1 ? '文章修改成功！' : '文章修改失败，请重试。' ?>
      </div>
    <?php endif; ?>
    <form action="../control/updateArt.php" method="post">
      <input type="hidden" name="aid" value="<?= htmlspecialchars($_GET['aid']) ?>">

      <label for="title">文章标题：</label>
      <input type="text" id="title" name="title" value="<?= htmlspecialchars($article['title']) ?>" required>

      <label for="category">文章所属板块：</label>
      <select id="category" name="board" required>
        <?php foreach ($category as $val): ?>
          <option value="<?= htmlspecialchars($val['board_id']) ?>" <?= $artBoard['board_name'] === $val['board_name'] ? 'selected' : '' ?>>
            <?= $val['board_name'] ?>
          </option>
        <?php endforeach; ?>
      </select>

      <label for="content">文章内容：</label>
      <textarea id="content" name="text" required><?= htmlspecialchars($article['content']) ?></textarea>

      <div class="btn-group">
        <button type="submit" class="btn btn-primary">保存修改</button>
        <button type="reset" class="btn btn-danger">重置内容</button>
      </div>
    </form>
  </div>
</body>
<script>
  document.querySelector('.header span').addEventListener('click', () => {
    let aid = new URLSearchParams(window.location.search).get('aid')
    window.location.href = `./userindex.php?aid=${aid}`
  })
</script>

</html>